package com.alibaba.citrus.ots.application.report.ability.repository.impl;

import com.alibaba.citrus.ots.application.report.ability.repository.MonthSalesCategoryReportWriteRepository;
import com.alibaba.lattice2.epoch.sdk.model.Result;
import com.epoch.app.otsapplicationreportsdk.model.dto.MonthSalesBasicReportSDO;
import com.epoch.app.otsapplicationreportsdk.monthsalescategoryreport.dto.UpdateResponse;
import com.epoch.app.otsapplicationreportsdk.monthsalescategoryreport.model.MonthSalesCategoryReport;
import com.epoch.app.otsapplicationreportsdk.monthsalescategoryreport.service.MonthSalesCategoryReportService;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.Map;

/**
 * @author zhoubc
 * @description: TODO
 * @date 2021/12/10 23:06
 */
@Component
public class MonthSalesCategoryReportWriteRepositoryImpl implements MonthSalesCategoryReportWriteRepository {

    @Resource
    MonthSalesCategoryReportService monthSalesCategoryReportService;

    @Override
    public String add(MonthSalesBasicReportSDO sdo) {
        MonthSalesCategoryReport monthSalesCategoryReport = new MonthSalesCategoryReport();
        BeanUtils.copyProperties(sdo,monthSalesCategoryReport);
        Map m = (Map)monthSalesCategoryReportService.create(monthSalesCategoryReport);
        Map result = (Map)m.get("result");
        return (String)result.get("lastInsertId");
    }

    @Override
    public Integer update(MonthSalesBasicReportSDO sdo) {
        MonthSalesCategoryReport monthSalesCategoryReport = new MonthSalesCategoryReport();
        BeanUtils.copyProperties(sdo,monthSalesCategoryReport);
        UpdateResponse response = monthSalesCategoryReportService.update(monthSalesCategoryReport);
        return response.getCount();
    }

    @Override
    public Result addOrUpdate(MonthSalesBasicReportSDO sdo) {
        MonthSalesCategoryReport monthSalesCategoryReport = new MonthSalesCategoryReport();
        BeanUtils.copyProperties(sdo,monthSalesCategoryReport);
        UpdateResponse response = monthSalesCategoryReportService.update(monthSalesCategoryReport);
        if(response.getCount() == 0){
            Map m = (Map)monthSalesCategoryReportService.create(monthSalesCategoryReport);
            Map result = (Map)m.get("result");
            return Result.success(response.getCount(),(String)result.get("lastInsertId"));
        }
        return Result.success(response.getCount());
    }

}
